//
//  NewConfigController.m
//  MobilBlogg
//
//  Created by Tobias Rundström on 2009-11-09.
//  Copyright 2009 Purple Scout. All rights reserved.
//


#import "UserConfigController.h"
#import "NeedishStore.h"
#import "INDefaultStyleSheet.h"
#import "MyNeedsResultsModel.h"
#import "MyNeedsViewDataSource.h"


static CGFloat kImageWidth = 97;
static CGFloat kImageHeight = 25;


@implementation UserConfigController

@synthesize username, password, loadingLabel;

- (id)init {
	if (self = [super init]) {
		self.view.frame = TTScreenBounds();
		self.view.backgroundColor = TTSTYLEVAR(needishBackgroundColor);
		self.autoresizesForKeyboard = YES;
		self.hidesBottomBarWhenPushed = YES;

		TTImageView* _imageView = [[[TTImageView alloc] initWithFrame:CGRectZero] autorelease];
		_imageView.image = [UIImage imageNamed:@"dev-needish.png"];	 
		
		CGFloat viewWidth = 320;
		CGFloat viewHeight = 40;
		
		CGFloat x = viewWidth/2 - kImageWidth/2;
		CGFloat y = 40;
		CGFloat padding = 20;
		
		UIColor* black = [UIColor grayColor];// RGBCOLOR(158, 163, 172);
		
		
		_imageView.frame = CGRectMake(x, y + padding , kImageWidth, kImageHeight);
		
		
		CGRect frameuser = CGRectMake(10, y + kImageHeight + padding*2, viewWidth - 20, viewHeight);
		TTView* viewuser = [[[TTView alloc] initWithFrame:frameuser] autorelease];
		viewuser.backgroundColor = TTSTYLEVAR(needishBackgroundColor);
		
		viewuser.style =     [TTShapeStyle styleWithShape:
							  [TTRoundedRectangleShape shapeWithTopLeft:10 topRight:10 bottomRight:0 bottomLeft:0] next:
							  [TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
							   [TTSolidBorderStyle styleWithColor:black width:1 next:nil]]];
		
		CGRect framepass = CGRectMake(10, viewuser.bottom -1 , viewWidth - 20, viewHeight);
		TTView* viewpass = [[[TTView alloc] initWithFrame:framepass] autorelease];
		viewpass.backgroundColor = TTSTYLEVAR(needishBackgroundColor);	  
		
		viewpass.style =     [TTShapeStyle styleWithShape:
							  [TTRoundedRectangleShape shapeWithTopLeft:0 topRight:0 bottomRight:10 bottomLeft:10] next:
							  [TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
							   [TTSolidBorderStyle styleWithColor:black width:1 next:nil]]];
		
		
		[viewuser addSubview:self.username];
		[viewpass addSubview:self.password];
		
		[self.view addSubview:_imageView];
		[self.view addSubview:viewuser];
		[self.view addSubview:viewpass];
		[self.view addSubview:self.loadingLabel];
		
		[self.username becomeFirstResponder];
		[self.password becomeFirstResponder];
	}
	return self;
}

- (void)viewWillAppear:(BOOL)animated
{
	[self.navigationController.navigationBar setHidden:TRUE];
}


#pragma mark --
#pragma mark UITextField

- (UITextField *)username
{
	if (username == nil)
	{
		CGRect frame = CGRectMake(10, 10, 285, 24);
		username = [[UITextField alloc] initWithFrame:frame];
		username.placeholder = @"Email";
		username.text = [NeedishStore getUserName];
		username.font = TTSTYLEVAR(font);
		username.returnKeyType = UIReturnKeyGo;
		username.clearButtonMode = UITextFieldViewModeWhileEditing;
		username.keyboardType = UIKeyboardTypeEmailAddress;
		username.delegate = self;
		
	}	
	return username;
}


- (UITextField *)password
{
	if (password == nil)
	{
		CGRect frame = CGRectMake(10, 10, 285, 24);
		password = [[UITextField alloc] initWithFrame:frame];
		password.placeholder = @"Password";
		password.secureTextEntry = YES;
		password.font = TTSTYLEVAR(font);
		password.returnKeyType = UIReturnKeyGo;
		password.clearButtonMode = UITextFieldViewModeWhileEditing;
		password.delegate = self;
	}	
	return password;
}


- (TTActivityLabel *)loadingLabel
{
	if (loadingLabel == nil)
	{
		CGRect frame = CGRectMake(11, 113, 298, 63);
		loadingLabel = [[[TTActivityLabel alloc] initWithFrame:frame] autorelease];
		loadingLabel.text = @"Loading...";
		[self.loadingLabel setHidden:YES];
	}	
	return loadingLabel;
}

#pragma mark -
#pragma mark Interactive

- (void)showLoading:(BOOL)loading {
	if (loading) {
		[self.loadingLabel setHidden:FALSE];
	}
	else {
		[self.loadingLabel setHidden:YES];
	}
}

-(void)clearData
{
	TTLOG(@"Removing stored data");
	[NeedishStore removeAllData];
	[self dismissModalViewController];
}

-(BOOL)textFieldShouldReturn:(UITextField*)field
{
	if (field == username) {
		[username resignFirstResponder];
		[password becomeFirstResponder];
	} else if (field == password) {
		[self saveSettings];
	}
	return YES;
}


-(void)saveSettings
{
	TTLOG(@"username = %@ and password = %@", username.text, password.text);
	NeedishLogin *login = [NeedishLogin loginWithUsername:username.text andPassword:password.text];
	login.delegate = self;
	[self showLoading:YES];
}

-(void)loginDidSucceed
{
	[NeedishStore saveUserName:username.text];
	[NeedishStore savePassword:password.text forUsername:username.text];
		
	[self showLoading:NO];
	[self dismissModalViewControllerAnimated:YES];
	TTLOG(@"-------Loading my needs");
//	self.superController
}

-(void)loginDidFailWithError:(NSError *)err
{
	[self showLoading:NO];

	UIAlertView *alview = [[[UIAlertView alloc] initWithTitle:@"Login Failed"
												   message:[err localizedDescription]
												  delegate:self
										 cancelButtonTitle:[[err localizedRecoveryOptions] objectAtIndex:0]
										 otherButtonTitles:nil] autorelease];
	[alview show];
	[username becomeFirstResponder];
}

//- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
//{
//}

-(void)dealloc
{
	[password release];
	[username release];
	[loadingLabel release];
	[super dealloc];
}

@end
